#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int a[55];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,k;
		memset(a,0,sizeof(a));
		scanf("%d %d %d",&n,&m,&k);
		int num = n/k;
		a[0] = min(num,m);
		//printf("****%d\n",a[0]);
		m = m-a[0];
		for(int i = 1;m>0;m--,i++)
		{
			if(i%k==0) i++;
			a[i%k]++;
		}
		sort(a,a+k);
		printf("%d\n",a[k-1]-a[k-2]);
	}
} 
